"""
Case Type   : DSS元数据修复工具
Case Name   : dsstbox ssrepair参数修改元数据类型为root_ft_block
Create At   : 2025/04
Owner       : haomeng
Description :
    1、查看当前卷名
    2、使用dsscmd showdisk查看root_ft_block类型的元数据值
    3、修改部分不支持修改元数据
    4、修改部分支持修改元数据
Expect      :
    1、查看成功
    2、查看成功
    3、修改失败
    4、修改成功
History     :
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DSSrepairtools(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.user_node = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()

        self.vol_path = os.path.join(macro.DSS_INSTANCE_PATH, 'cfg/dss_vg_conf.ini')
        self.metadata = ['ft_block.common.checksum', 'ft_block.common.type', 'ft_block.common.version',
                         'ft_block.common.id', 'ft_block.common.flags', 'ft_block.node_num', 'ft_block.next',
                         'ft_root.free_list.count', 'ft_root.free_list.first', 'ft_root.free_list.last',
                         'ft_root.items.count', 'ft_root.items.first', 'ft_root.items.last',
                         'ft_root.fid', 'ft_root.first', 'ft_root.last']
        self.metadata_value = [1185018548, 0, 125, 0, 0, 1, 17408, 8094, 1724034232370176, 144097595889828864,
                               8094, 1724034232370176, 144097595889828864, 105, 0, 144097595889828864]
        self.root_ft_block = []

    def test_dsstbox(self):
        text = '-----step1:查看当前卷名;  expect:查看成功-----'
        self.log.info(text)

        res = self.user_node.sh(f"cat {self.vol_path} | awk 'NR==1'").result()
        self.log.info(res)
        vol_name = res.split(':')[0]
        vol_value = res.split(':')[1]
        self.log.info(f'vol_name的值是：{vol_name}, vol_value的值是：{vol_value}')
        self.assertNotEqual('', vol_name, '查看当前卷名失败')

        text = '-----step2:使用dsscmd showdisk查看root_ft_block类型的元数据值;  expect:修改成功-----'
        self.log.info(text)

        cmd = f"source {macro.DB_ENV_PATH};dsscmd showdisk -g {vol_name} -s root_ft_block"
        self.log.info(cmd)
        execute_cmd = self.user_node.sh(cmd).result()
        self.log.info(execute_cmd)
        self.root_ft_block.append(execute_cmd.splitlines())
        self.log.info(self.root_ft_block)

        sum_ft_block = sum(len(item) for sublist in self.root_ft_block for item in sublist)
        self.log.info(sum_ft_block)
        self.assertIn('Succeed to print dss metadata', execute_cmd, '查看元数据结果失败')

        text = '-----step3:修改部分不支持修改元数据;  expect:修改失败-----'
        self.log.info(text)

        for param in self.metadata[:1]:
            cmd1 = f"dsstbox ssrepair -v {vol_value} -t root_ft_block -k '{param}={self.metadata_value[self.metadata.index(param)]}'"
            self.log.info(cmd1)
            execute_cmd1 = f'''source {macro.DB_ENV_PATH};yes|{cmd1}'''
            self.log.info(execute_cmd1)
            res1 = self.user_node.sh(execute_cmd1).result()
            self.log.info(res1)
            self.assertIn('Failed to execute repair meta info', res1, '修改元数据成功')

        text = '-----step4:修改部分支持修改元数据;  expect:修改成功-----'
        self.log.info(text)

        for param in self.metadata[1:]:
            cmd2 = f"dsstbox ssrepair -v {vol_value} -t root_ft_block -k '{param}={self.metadata_value[self.metadata.index(param)]}'"
            self.log.info(cmd2)
            execute_cmd2 = f'''source {macro.DB_ENV_PATH};yes|{cmd2}'''
            self.log.info(execute_cmd2)
            res2 = self.user_node.sh(execute_cmd2).result()
            self.log.info(res2)
            self.assertIn('Succeed to execute repair meta info', res2, '修改元数据失败')

    def tearDown(self):
        self.log.info('-----No need to clean-----')
        self.log.info(f'----{os.path.basename(__file__)} end----')
